// pages/sqzp/sqzp.js
const app = getApp()
var util = require('../../utils/time.js')
// const cloud = require('wx-server-sdk')
// cloud.init()
// const db = cloud.database()
// const _ = db.command
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nameinput: '',
        phonenum: '',
        address: '',
        requirement: '',
        check2: false,
        admin_send: '',
        user_send: ''

    },

    onLoad: function (options) {
        var openid = app.globalData.openid
        let type = app.globalData.type
        wx.cloud.callFunction({
            name: 'userget',
            data: {
                type,
                openid
            },
            success: res => {
                app.globalData.phonenum = res.result.data[0].phonenum
            }
        })
    },
    //姓名输入
    input1: function (data) {
        this.setData({
            nameinput: data.detail.value
        })
    },

    //手机号码输入
    input2: function (data) {
        this.setData({
            phonenum: data.detail.value
        })
    },
    //地址输入
    bindTextAreaBlur: function (data) {
        this.setData({
            address: data.detail.value
        })
    },

    //赠品需求输入
    bindTextAreaBlur2: function (data) {
        this.setData({
            requirement: data.detail.value
        })
    },

    radiocon: function (e) {
        this.setData({
            check2: !this.data.check2
        })
        console.log("用户打勾的是 ", this.data.check2)
    },

    infoform(res) {
        var {
            name,
            phonenum,
            address,
            requirement,
        } = res.detail.value;
        var openid = app.globalData.openid;
        let that = this
        setTimeout(function () {
                //判定输入项是否为空
                if (!that.data.nameinput) {
                    wx.showToast({
                        title: '请填写姓名',
                        icon: 'none'
                    })
                } else if (!that.data.phonenum) {
                    wx.showToast({
                        title: '请填写手机号码',
                        icon: 'none'
                    })
                } else if (!that.data.address) {
                    wx.showToast({
                        title: '请填写地址',
                        icon: 'none'
                    })
                } else if (!that.data.requirement) {
                    wx.showToast({
                        title: '请填写需求',
                        icon: 'none'
                    })
                } else if (that.data.check2 == false) {
                    wx.showToast({
                        title: '请勾选确认框',
                        icon: 'none'
                    })
                } else {
                    //读取信息输入库，如已有身份信息则更新；如未有身份信息则添加新信息
                    var sqrname, sqrphone, sqrsex;
                    let time = util.formatTime(new Date())
                    let type = app.globalData.type
                    wx.cloud.callFunction({
                            name: 'userget',
                            data: {
                                type,
                                openid
                            },
                            success: res => {
                                console.log('获取数据库用户信息:', res)
                                sqrname = res.result.data[0].name
                                sqrphone = res.result.data[0].phonenum
                                sqrsex = res.result.data[0].sex
                                wx.cloud.callFunction({
                                    name: 'zpsqadd',
                                    data: {
                                        name1: sqrname,
                                        phonenum1: sqrphone,
                                        sex: sqrsex,
                                        name2: name,
                                        phonenum2: phonenum,
                                        address: address,
                                        wgift: requirement,
                                        time,
                                        admin_send: '',
                                        user_send: ''
                                    },
                                    success: ress => {
                                        console.log('添加新赠品申请信息: ', ress)
                                        wx.redirectTo({
                                            url: '/pages/sqzp2/sqzp2',
                                        })
                                    },
                                    fail: ress => {
                                        console.log('添加新赠品申请信息失败: ', ress)
                                    }
                                })
                            },
                            fail: res => {
                                console.log('获取数据库用户信息失败:', res)
                                wx.showToast({
                                    title: '您未有采集记录',
                                    icon: 'error'
                                })
                            }
                        }),
                        console.log('赠品申请信息：', that.data);
                }
            },
            100)

    },
    jilu() {
        wx.navigateTo({
            url: '/pages/sqzp2/sqzp2',
        })
    }
})